package com.technology.xml;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXParserUsing extends DefaultHandler {

	private static SAXParserUsing instance = new SAXParserUsing();
	private static Note note = new Note();
	
	public static void main(String[] args) {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try {
			SAXParser parser = factory.newSAXParser();
			parser.parse(new File("src/com/technology/xml/note.xml"), instance);
			System.out.println("Sender :" + note.getSender());
			System.out.println("Recipient :" + note.getRecipient());
			System.out.println("Subject :" + note.getSubject());
			System.out.println("Body :" + note.getText());
			System.out.println("Note Date :" + note.getNoteDate());
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

//	@Override
//	public void startDocument() throws SAXException {
//		System.out.println("START DOCUMENT");
//	}
//
//	@Override
//	public void endDocument() throws SAXException {
//		System.out.println("END DOCUMENT");
//	}

	private boolean isTo, isFrom, isHeading, isBody;
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (qName.equalsIgnoreCase("note")){
			String date = attributes.getValue("date");
			try {
				note.setNoteDate(new SimpleDateFormat("dd.MM.yyyy").parse(date));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		else if (qName.equalsIgnoreCase("to")){
			isTo = true;
		}
		else if (qName.equalsIgnoreCase("from")){
			isFrom = true;
		}
		else if (qName.equalsIgnoreCase("heading")){
			isHeading = true;
		}
		else if (qName.equalsIgnoreCase("body")){
			isBody = true;
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (qName.equalsIgnoreCase("to")){
			isTo = false;
		}
		else if (qName.equalsIgnoreCase("from")){
			isFrom = false;
		}
		else if (qName.equalsIgnoreCase("heading")){
			isHeading = false;
		}
		else if (qName.equalsIgnoreCase("body")){
			isBody = false;
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String value = new String(ch, start, length);
		if (isTo){
			note.setRecipient(value);
		}
		else if (isFrom){
			note.setSender(value);
		}
		else if (isHeading){
			note.setSubject(value);
		}
		else if (isBody){
			note.setText(value);
		}
	}

	
}
